package types;

import java.util.ArrayList;

import functions.NCD;

public class SubSet {

	private ArrayList<String> dataElements;
	private String bestMedoid;
	private Integer indexes[];
	
	// constructors
	public SubSet() {
		ArrayList<String> newList = new ArrayList<String>();
		this.dataElements = newList;
		this.bestMedoid = null;
		
	}
	public SubSet(ArrayList<String> data) {
		this.dataElements = data;
	}
	
	// getters and setters
	public String getElement(Integer i){
		return this.dataElements.get(i);
	}
	
	public void deleteElement(Integer i){
		this.dataElements.remove(i);
	}
	
	public int getTestIndex(int index){
		return indexes[index];
	}

	public SubSet getElementsAsSubSet(){
		SubSet sub = new SubSet();
		
		for(int i=0; i<this.dataElements.size(); i++)
			sub.setElement(this.dataElements.get(i));
			
		return sub;
	}

	public void setElement(String test){
		this.dataElements.add(test);
	}

	public void setElement(SubSet subset){
		for(int i=0; i<subset.getSize(); i++)
			this.dataElements.add(subset.getElement(i));
	}
	
	public void setBestMedoid(String test){
		this.bestMedoid = test;
	}
	
	public Integer getSize(){
		return this.dataElements.size();
	}
	
	public String getBestMedoid(){
		return this.bestMedoid;
	}
	
	public int getMedoidIndex(){
		for(int i=0; i< this.dataElements.size(); i++){
			if(this.dataElements.get(i).equals(this.bestMedoid))
				return this.getTestIndex(i);
		}
		return 0;
	}
	public void setSubSetIndexes(DataSet ds) {
		boolean cont = false;
		this.indexes = new Integer[dataElements.size()];
		for(int i=0; i<indexes.length; i++){
			String test = this.getElement(i);
			
			for(int j=0; j<ds.getSize(); j++){
				if(test.equals(ds.getElement(j))){
					cont = false;
					for(int k=0; k<i ; k++){
						if(indexes[k] == j)
							cont = true;
					}
					if(cont == false)
						indexes[i]=j;
				
				}	
			}
		}
		
	}
	
	
	public double[][] getDistanceMatrix() {
		int n = this.dataElements.size();
		double[][] result = new double[n][n];
		double dis = 0.0;
		for (int i = 0; i < n; i++) {
			for (int j = i; j < n; j++) {
				dis = NCD.getNCD(this.dataElements.get(i), this.dataElements.get(j));
				result[i][j] = dis;
				result[j][i] = dis;
			}
		}
		return result;
	}
	
	
}
